package cn.mntool.backup.server;

import cn.mntool.backup.config.BackupConfig;
import cn.mntool.backup.utils.*;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * describe:
 *
 * @author lianying
 * @date 2019/01/30
 */
public class MysqlBackupServer extends DbBackupServer {
    /**
     * log4j
     */
    private final Logger logger = Logger.getLogger(MysqlBackupServer.class);

    public MysqlBackupServer(BackupConfig backupConfig) {
        super(backupConfig);
    }

    @Override
    public void backup() {
        logger.info("备份启动");
        String command = formatCommand();
        //如果不存在就创建
        FileUtils.mkdirs(getBackupConfig().getDbSavePath());
        logger.info("执行备份命令："+command);
        RuntimeUtils.exec(command, new File(PathUtils.classPath() + "/dbBackupTools"));
        logger.info("备份成功");
    }
    @Override
    public void error(Exception e) {
        logger.error("备份失败:",e);
    }

    @Override
    public void uploadFtp() throws Exception {
        logger.error("暂时不支持ftp备份");
    }

    @Override
    public void close() {
        logger.info("备份关闭");
    }

    @Override
    public String formatCommand() {
        //生成文件名
        String command = PropertiesUtils.getCommandProperties().getMysqlCommand();
        return String.format(command, getBackupConfig().getDbHost(), getBackupConfig().getDbPort()
                , getBackupConfig().getDbUsername(), getBackupConfig().getDbPassword()
                , getBackupConfig().getDbSavePath() + "/" + getFileName(), getBackupConfig().getDbName());
    }
}
